<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html><head><title>Programming in Lua : 6</title>
<link rel="stylesheet" type="text/css" href="pil.css">
</head>
<body>
<p class="warning">
<A HREF="contents.html"><IMG TITLE="Programming in Lua (first edition)" SRC="capa.jpg" ALT="" ALIGN="left"></A>This first edition was written for Lua 5.0. While still largely relevant for later versions, there are some differences.<BR>The third edition targets Lua 5.2 and is available at <A HREF="http://www.amazon.com/exec/obidos/ASIN/859037985X/theprogrammil3-20">Amazon</A> and other bookstores.<BR>By buying the book, you also help to <A HREF="../donations.html">support the Lua project</A>.
</p>
<table width="100%" class="nav">
<tr>
<td width="10%" align="left"><a href="5.3.html"><img border="0" src="left.png" alt="Previous"></a></td>
<td width="80%" align="center"><a href="contents.html"><font face="Helvetica,Arial,sanserif">
<font color="gray">Programming in </font><font color="blue"> Lua</font>
</font></a></td>
<td width="10%" align="right"><a href="6.1.html"><img border="0" src="right.png" alt="Next"></a></td>
</tr>
<tr>
<td width="10%" align="left"></td>
<td width="80%" align="center"><a href="contents.html#P1">Part I. The Language</a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="contents.html#6">Chapter 6. More about Functions</a></td>
<td width="10%" align="right"></td></tr>
</table>
<hr/>
<a name="firstclass"><h1>6 &ndash; More about Functions</h1></a>

<p>Functions in Lua are first-class values
with proper lexical scoping.

<p>What does it mean for functions to be "first-class values"?
It means that, in Lua, a function is a value
with the same rights as conventional values like
numbers and strings.
Functions can be stored in variables (both global and local) and
in tables, can be passed as arguments,
and can be returned by other functions.

<p>What does it mean for functions to have "lexical scoping"?
It means that functions can access variables of its enclosing functions.
(It also means that Lua contains the lambda calculus properly.)
As we will see in this chapter,
this apparently innocuous property brings great power to the language,
because it allows us to apply in Lua
many powerful programming techniques
from the functional-language world.
Even if you have no interest at all in functional programming,
it is worth learning a
little about how to explore those techniques,
because they can make your programs smaller and simpler.

<p>A somewhat difficult notion in Lua is that functions,
like all other values, are anonymous;
they do not have names.
When we talk about a function name, say <code>print</code>,
we are actually talking about a variable that holds that function.
Like any other variable holding any other value,
we can manipulate such variables in many ways.
The following example, although a little silly,
shows the point:
<pre>
    a = {p = print}
    a.p("Hello World") --> Hello World
    print = math.sin  -- `print' now refers to the sine function
    a.p(print(1))     --> 0.841470
    sin = a.p         -- `sin' now refers to the print function
    sin(10, 20)       --> 10      20
</pre>
Later we will see more useful applications for this facility.

<p>If functions are values,
are there any expressions that create functions?
Yes.
In fact, the usual way to write a function in Lua,
like
<pre>
    function foo (x) return 2*x end
</pre>
is just an instance of what we call <em>syntactic sugar</em>;
in other words, it is just a pretty way to write
<pre>
    foo = function (x) return 2*x end
</pre>
That is, a function definition is in fact a statement
(an assignment, more specifically)
that assigns a value of type <code>"function"</code> to a variable.
We can see the expression <code>function (x) ... end</code>
as a function constructor,
just as <code>{}</code> is a table constructor.
We call the result of such function constructors an
<em>anonymous function</em>.
Although we usually assign functions to global names,
giving them something like a name,
there are several occasions when functions remain anonymous.
Let us see some examples.

<p>The table library provides a function <code>table.sort</code>,
which receives a table and sorts its elements.
Such a function must allow unlimited variations in the sort order:
ascending or descending, numeric or alphabetical,
tables sorted by a key, and so on.
Instead of trying to provide all kinds of options,
<code>sort</code> provides a single optional parameter,
which is the <em>order function</em>:
a function that receives two elements
and returns whether the first must come before
the second in the sort.
For instance, suppose we have a table of records such as
<pre>
     network = {
       {name = "grauna",  IP = "210.26.30.34"},
       {name = "arraial", IP = "210.26.30.23"},
       {name = "lua",     IP = "210.26.23.12"},
       {name = "derain",  IP = "210.26.23.20"},
     }
</pre>
If we want to sort the table by the field <code>name</code>,
in reverse alphabetical order,
we just write
<pre>
    table.sort(network, function (a,b)
      return (a.name > b.name)
    end)
</pre>
See how handy the anonymous function is in that statement.

<p>A function that gets another function as an argument,
such as <code>sort</code>,
is what we call a <em>higher-order function</em>.
Higher-order functions are a powerful programming mechanism
and the use of anonymous functions
to create their function arguments is a great source of flexibility.
But remember that
higher-order functions have no special rights;
they are a simple consequence of the ability of Lua to handle
functions as first-class values.

<p>To illustrate the use of functions as arguments,
we will write a naive implementation of
a common higher-order function,
<code>plot</code>, that plots a mathematical function.
Below we show this implementation,
using some escape sequences to draw on an ANSI terminal.
(You may need to change these control sequences
to adapt this code to your terminal type.)
<pre>
    function eraseTerminal ()
      io.write("\27[2J")
    end
</pre>
<pre>
    -- writes an `*' at column `x' , row `y'
    function mark (x,y)
      io.write(string.format("\27[%d;%dH*", y, x))
    end
</pre>
<pre>
    -- Terminal size
    TermSize = {w = 80, h = 24}
    
    -- plot a function
    -- (assume that domain and image are in the range [-1,1])
    function plot (f)
      eraseTerminal()
      for i=1,TermSize.w do
         local x = (i/TermSize.w)*2 - 1
         local y = (f(x) + 1)/2 * TermSize.h
         mark(i, y)
      end
      io.read()  -- wait before spoiling the screen
    end
</pre>
With that definition in place,
you can plot the sine function with a call like
<pre>
    plot(function (x) return math.sin(x*2*math.pi) end)
</pre>
(We need to massage the data a little to put values in the proper range.)
When we call <code>plot</code>,
its parameter <code>f</code> gets the value of
the given anonymous function,
which is then called inside the <b>for</b> loop repeatedly
to provide the values for the plotting.

<p>Because functions are first-class values in Lua,
we can store them not only in global variables,
but also in local variables and in table fields.
As we will see later,
the use of functions in table fields is a key ingredient
for some advanced uses of Lua,
such as packages and object-oriented programming.

<hr/>
<table width="100%" class="nav">
<tr valign="top">
<td width="90%" align="left">
<small>
  Copyright &copy; 2003&ndash;2004 Roberto Ierusalimschy.  All rights reserved.
</small>
</td>
<td width="10%" align="right"><a href="6.1.html"><img border="0" src="right.png" alt="Next"></a></td>
</tr>
</table>


</body></html>

